package com.senthink.gl.business.domain.service;

import com.senthink.gl.business.common.exception.DataAccessException;
import com.senthink.gl.business.domain.entity.UserMenuAuthority;
import com.senthink.gl.business.domain.repository.UserMenuAuthorityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户菜单权限DataService
 *
 * @author zooqi@senthink.com
 * @date 2018/03/05
 */
@Service
public class UserMenuAuthorityDataService {

    @Autowired
    private UserMenuAuthorityRepository repository;

    /**
     * 保存/更新记录
     *
     * @param authority 需要保存/更新的实体
     * @return
     */
    public UserMenuAuthority save(UserMenuAuthority authority) {
        try {
            return repository.save(authority);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 批量保存记录
     *
     * @param authorities 需要保存的实体
     */
    public List<UserMenuAuthority> saveAll(Iterable<UserMenuAuthority> authorities) {
        try {
            return repository.save(authorities);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 通过uid批量删除
     *
     * @param uid
     * @return
     */
    public int deleteAllByUid(String uid) {
        try {
            return repository.deleteAllByUserUid(uid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }
}
